package com.deam.scorpio.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.deam.imsdk.imsdk.IMManager;
import com.deam.imsdk.imsdk.RequestResultListener;
import com.deam.imsdk.imsdk.TIMCallBack;
import com.deam.imsdk.utils.Constants;
import com.deam.scorpio.R;
import com.deam.scorpio.ui.dialog.LoadingDialog;
import com.qbkj.base_lib.ui.BaseActivity;

import com.alibaba.fastjson.JSONObject;

public class LoaginActivity extends BaseActivity implements RequestResultListener {
    private LoadingDialog mLoadingDialog;

    @Override
    protected int setContentLayoutId() {
        return R.layout.activity_loagin;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        v.setText(R.id.back_btn, "");
        v.setText(R.id.menu_btn, "注册");
        mLoadingDialog = new LoadingDialog(this);
        IMManager.getInstance().registerRequestReturn(this);
        v.setOnClickListener(this, R.id.login_btn);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        IMManager.getInstance().unRegisterRequestReturn(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.login_btn:
                mLoadingDialog.show();
                EditText user_edit = this.v.getEditText(R.id.user_edit);
                IMManager.getInstance().login(this, user_edit.getText().toString());
                break;
            case R.id.menu_btn:
                skipActivity(RegisterActivity.class, 1000, new Intent(), null);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String phone = data.getStringExtra("phone");
        if (phone != null) {
            IMManager.getInstance().login(this, phone);
        }
    }

    @Override
    public void onError(int error, String info) {
        mLoadingDialog.dismiss();
        toast(info);
    }

    @Override
    public void onSuccess(int reqId, JSONObject json) {
        if (reqId == Constants.ACTION_LOGING) {
            mLoadingDialog.dismiss();
            toast("登录成功");
            skipActivityFinish(MainActivity.class);
        }
    }
}
